package model;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MediaFile {

	private File file;

	private Integer series;

	private Integer episode;

	private String name;

	public MediaFile(File file) {
		super();
		this.file = file;
		this.name = file.getName();
		setSeriesAndEpisode();
	}

	private void setSeriesAndEpisode() {
		String input = name.trim().toLowerCase();
		//		System.out.println(input + "-");
		Pattern pattern1 = Pattern.compile("s[0-9]+e[0-9]+");
		Matcher matcher = pattern1.matcher(input);
		if (matcher.find()) {
			String found = input.substring(matcher.start(), matcher.end()).trim();
			series = Integer.valueOf(found.substring(1, found.indexOf('e')).trim());
			episode = Integer.valueOf(found.substring(found.indexOf('e') + 1).trim());
			//			System.out.println(series + "," + episode);

		} else {
			pattern1 = Pattern.compile("[0-9]+x[0-9]+");
			matcher = pattern1.matcher(input);
			if (matcher.find()) {
				String found = input.substring(matcher.start(), matcher.end()).trim();
				series = Integer.valueOf(found.substring(0, found.indexOf('x')).trim());
				episode = Integer.valueOf(found.substring(found.indexOf('x') + 1).trim());
				//				System.out.println(found + " " + series + "," + episode);

			} else {
				// System.out
				// .println("Warning could not determine series and episode for:"
				// + name);
			}
		}
	}

	public Integer getSeries(String name) {

		return episode;

	}

	public Integer getEpisode(String name) {

		return episode;

	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getSeries() {
		return series;
	}

	public void setSeries(Integer series) {
		this.series = series;
	}

	public Integer getEpisode() {
		return episode;
	}

	public void setEpisode(Integer episode) {
		this.episode = episode;
	}
}
